var infowindow = new google.maps.InfoWindow();
function lobo(id,leyenda) {
		this.leyenda = leyenda||'';
    this.id = id;
    this.actividad = [];
    this.pines = [];
    this.poly = new google.maps.Polyline(mapa.polylineOptions[this.id]);
    this.path = [];
    this.iconolobo = new google.maps.MarkerImage('/gps/2.0/ui/i/lobo.png', null, null, new google.maps.Point(8, 8), null);
    this.iconolobopast = new google.maps.MarkerImage('/gps/2.0/ui/i/pos.png', null, null, new google.maps.Point(3, 3), null);
    this.init = function() {
        this.poly.setMap(mapa.map);
        this.path = this.poly.getPath();
    };
    this.showInfoWindowEvent=function(i){
    	this.showInfoWindow(i);
    };
    this.showInfoWindow=function(i){
    	var p = this.pines[i].pin;
    	infowindow.setPosition(p.getPosition());
    	infowindow.setContent(p.getTitle());
    	infowindow.open(mapa.map,p);
    };
    this.actividadPasada = function() {
    		$('#loader').css({'display':'block'});
        var me = this;
        $.ajax({
            type: 'GET',
            url: '/gps/2.0/ui/ws/prc.php',
            datatype: 'application/json',
            data: "a=alldata&l=" + this.id + "&i=0",
            success: function(jsondata) {
                if (jsondata.length > 0) {
                    me.actividad = JSON.parse(jsondata);
                    me.crearPines(true, 'multi');
    								$('#loader').css({'display':'none'});
                }
            },
            error: function(err) {}
        });
    };
    this.actividadActual = function() {
    		var me = this;
        var ultimoElemento = me.actividad.length - 1;
        if (typeof this.actividad[ultimoElemento] === 'object') {
            var GETData = "a=current&l=" + this.id + "&i=" + me.actividad[ultimoElemento].id;
        } else {
            var GETData = "a=current&l=" + this.id + "&i=" + (JSON.parse(me.actividad[ultimoElemento]))[0].id;
        }
        $.ajax({
            type: 'GET',
            url: '/gps/2.0/ui/ws/prc.php',
            datatype: 'application/json',
            data: GETData,
            success: function(data) {
                data = JSON.parse(data);
                if (data.length > 0) {
                    data = data[0];
                    var i = me.actividad.length;
                    while (i--) {
                        if (data.id == me.actividad[i].id) {
                            return false;
                        }
                    }
                    me.actividad.push(data);
                    me.crearPines(true, 'single', data);
                }
            },
            error: function(err) {}
        });
    };
    this.crearPines = function(pinVisible, tipoCarga, dataSingle) {
        var me = this;
        var fin;
        if (tipoCarga === 'multi') {
            var total = this.actividad.length;
            if(total>0){
            		var i = total;
            		while(i--){
		            //for (i = 0; i < total; i++) {
		                var f = this.actividad[i];
		                var pos = new google.maps.LatLng(f.lat, f.lon);
		                var icono = i == (total - 1) ? this.iconolobo: this.iconolobopast;
		                var contenido=f.fecha + ' @ ' + pos.toString();
		                var pin = {
		                    posicion: pos,
		                    pin: new google.maps.Marker({
		                        position: pos,
		                        map: mapa.map,
		                        title: contenido,
		                        icon: icono,
		                        visible: true
		                    })
		                };
		                this.pines.push(pin);
		                this.path.push(pin.posicion);
		            }
		            mapa.map.panTo(this.pines[total - 1].posicion);
          	}
        } else {
            var f = dataSingle;
            var pos = new google.maps.LatLng(f.lat, f.lon);
            var pin = {
                posicion: pos,
                pin: new google.maps.Marker({
                    position: pos,
                    map: mapa.map,
                    title: f.fecha + ' @ ' + pos.toString(),
                    icon: me.iconolobo,
                    visible: pinVisible
                })
            };
            this.pines[this.pines.length - 1].pin.setIcon(new google.maps.MarkerImage('/gps/2.0/ui/i/normalPast' + this.id + '.png', null, null, new google.maps.Point(8, 8), null));

            google.maps.event.addListener(pin.pin, 'click', function() {
            	infowindow.close();
            	infowindow.setContent(me.leyenda);
						  infowindow.open(mapa.map, pin.pin);
						});
						
            this.pines.push(pin);
            this.path.push(pin.posicion);
        }
    };
    this.sim_id = null;
    this.sim_estado = 'paused';
    this.control = function() {
        if (this.sim_estado == 'paused') {
            this.sim_estado = 'playing';
            this.play();
        } else {
            this.sim_estado = 'paused';
            this.pause();
        }
    };
    this.play = function() {
        this.init();
        this.actividadPasada();
        /*this.sim_id = setInterval('lobo'+this.id + '.actividadActual()', 10000);*/
    };
    this.pause = function() {
        clearInterval(this.sim_id);
        var i = this.pines.length;
        while(i--){
        	//for (i = 0; i < this.pines.length; i++) {
          this.pines[i].pin.setMap(null);
        }
        this.pines = [];
        this.poly.setMap(null);
        this.poly = new google.maps.Polyline(mapa.polylineOptions[this.id]);
        this.path = [];
        this.actividad = [];
        return false;
    }
}